package com.message.api.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.StringUtils;
import com.message.api.base.dao.MsTextMapper;
import com.message.api.base.entity.MsText;
import com.message.api.base.service.MsTextService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-08-10
 */
@Service
public class MsTextServiceImpl extends ServiceImpl<MsTextMapper, MsText> implements MsTextService {

    @Override
    public List<MsText> list(MsText example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<MsText> page(MsText example, IPage page) {
        return this.page(page, buildWrapper(example));
    }

    @Override
    public MsText save(String content) {
        MsText msText = new MsText();
        String code   = UUID.randomUUID().toString().replaceAll("-", "");
        msText.setCode(code);
        msText.setText(content);
        super.save(msText);
        return this.baseMapper.findByCode(code);
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private LambdaQueryWrapper<MsText> buildWrapper(MsText example) {
        LambdaQueryWrapper<MsText> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(example.getTitle()),MsText::getTitle,example.getTitle());
        wrapper.orderByDesc(MsText::getTextState);
        return wrapper;
    }



}
